<template>
  <div class="app-container">
    <h2 style="text-align: center">发布新课程</h2>
    <el-steps
      :active="1"
      process-status="wait"
      align-center
      style="marginbottom: 40px"
    >
      <el-step title="填写课程基本信息" />
      <el-step title="创建课程大纲" />
      <el-step title="最终发布" />
    </el-steps>
    <el-form label-width="120px">
      <el-form-item label="课程标题">
        <el-input
          v-model="courseInfo.title"
          placeholder="示例：机器学习项目课：从基础到搭建项目"
        ></el-input>
      </el-form-item>
      <!-- 所属分类FFFF -->
      <!-- 所属分类：级联下拉列表 -->
      <!-- 一级分类 -->
      <el-form-item label="课程类别">
        <el-select
          v-model="courseInfo.subjectParentId"
          @change="getTwoSubjectChange"
          placeholder="课程类别"
        >
          <el-option
            v-for="subject in subjectOneList"
            :key="subject.id"
            :label="subject.title"
            :value="subject.id"
          />
        </el-select>
        <!-- 二级分类 -->
        <el-select v-model="courseInfo.subjectId" placeholder="二级分类">
          <el-option
            v-for="subject in subjectTwoList"
            :key="subject.value"
            :label="subject.title"
            :value="subject.id"
          />
        </el-select>
      </el-form-item>

      <!-- 课程讲师 -->
      <el-form-item label="课程讲师">
        <el-select v-model="courseInfo.teacherId" placeholder="请选择">
          <el-option
            v-for="teacher in teacherList"
            :key="teacher.id"
            :label="teacher.name"
            :value="teacher.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="总课时"
        ><el-input-number
          controls-position="right"
          v-model="courseInfo.lessonNum"
          :min="0"
          placeholder="请输入总课时"
        ></el-input-number
      ></el-form-item>
      <!-- 课程简介 -->
      <!-- 课程简介-->
      <el-form-item label="课程简介">
        <tinymce :height="300" v-model="courseInfo.description" />
      </el-form-item>
      <!-- <el-form-item label="课程简介">
        <el-input
          v-model="courseInfo.description"
          placeholder="请输入课程简介"
        ></el-input>
      </el-form-item> -->
      <!-- 课程封面 -->
      <el-form-item label="课程价格">
        <el-input-number
          :min="0"
          v-model="courseInfo.price"
          controls-position="right"
          placeholder="请输入课程价格"
        >
        </el-input-number>
      </el-form-item>
      <el-form-item>
        <el-button
          :disabled="saveBtnDisabled"
          type="primary"
          @click="saveOrUpdate"
          >保存</el-button
        >
      </el-form-item>
      <!-- 课程封面 -->
      <el-form-item label="课程封面">
        <!-- 头衔缩略图 -->
        <pan-thumb :image="courseInfo.cover + ''" />
        <!-- 文件上传按钮 -->
        <el-button
          type="primary"
          icon="el-icon-upload"
          @click="imagecropperShow = true"
          >更换头像
        </el-button>
        <!--
v-show：是否显示上传组件
:key：类似于id，如果一个页面多个图片上传控件，可以做区分
:url：后台上传的url地址
@close：关闭上传组件
@crop-upload-success：上传成功后的回调 -->
        <image-cropper
          v-show="imagecropperShow"
          :width="200"
          :height="200"
          :params="{ module: 'course' }"
          :key="imagecropperKey"
          :url="BASE_API + '/api/oss/file/upload'"
          field="file"
          @close="close"
          @crop-upload-success="cropSuccess"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          :disabled="saveBtnDisabled"
          type="primary"
          @click="saveOrUpdate"
          >保存并下一步</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import courseApi from "@/api/edu/course";
import teacherApi from "@/api/edu/teacher";
import subjectApi from "@/api/edu/subject";
import ImageCropper from "@/components/ImageCropper";
import PanThumb from "@/components/PanThumb";
import Tinymce from "@/components/Tinymce";
export default {
  components: { ImageCropper, PanThumb, Tinymce },
  data() {
    return {
      saveBtnDisabled: false, // 保存按钮是否禁用
      courseInfo: {
        id: "",
        title: "",
        subjectParentId: "",
        subjectId: "",
        teacherId: "",
        lessonNum: 0,
        description: "",
        cover:
          "http://y.gtimg.cn/music/photo_new/T002R300x300M000001yHQzz2rm3lk.jpg?n=1",
        price: 0,
      },
      // 讲师列表
      teacherList: [],
      // 一级分类
      subjectOneList: [],
      // 二级分类
      subjectTwoList: [],
      BASE_API: process.env.BASE_API, // 接口API地址
      imagecropperShow: false, // 是否显示上传组件
      imagecropperKey: 0, // 上传组件id

      imageUrl: "",
    };
  },
  created() {
    this.init();
    if (this.$route.params && this.$route.params.id) {
      this.courseInfo.id = this.$route.params.id;
      this.getEntityById();
    } else {
    }
  },
  watch:{
    $route(to, from){
      console.log('重置')
      this.courseInfo = {}
    }
  },
  methods: {
    init() {
      this.listEntity();
      this.getOneSubject();
    },
    // 保存或者修改
    saveOrUpdate() {
      if (this.courseInfo.id) {
        courseApi.updateEntity(this.courseInfo).then((res) => {
          this.$message({
            type: "success",
            message: "修改课程成功",
          });


          this.$router.push({ path: "/course/chapter/" + res.data.info });
          // this.$router.push({ name: "" + res.data.info });

        });
      } else {
        courseApi.saveEntity(this.courseInfo).then((res) => {
          this.$message({
            type: "success",
            message: "添加课程成功",
          });
          this.$router.push({ path: "/course/chapter/" + res.data.info });
        });
      }
    },
    // 查询所有的讲师
    listEntity() {
      teacherApi.listEntity().then((res) => {
        this.teacherList = res.data.info;
      });
    },
    // 根据id查询所有的讲师
    getEntityById() {
      courseApi.getEntityById(this.courseInfo.id).then((res) => {
        this.courseInfo = res.data.info;
        this.subjectOneList.forEach((item) => {
          // 判断该id是否等于
          if (item.id == this.courseInfo.subjectParentId) {
            // 获取二级分类
            this.subjectTwoList = item.children;
            return;
          }
        });
      });
    },
    // 获取一级分类
    getOneSubject() {
      subjectApi.getTreeEntity().then((res) => {
        this.subjectOneList = res.data.info;
      });
    },
    // 获取二级分类
    getTwoSubjectChange(value) {
      console.log("查询二级分类");
      this.subjectOneList.forEach((item) => {
        // 判断该id是否等于
        if (item.id == value) {
          // 获取二级分类
          this.subjectTwoList = item.children;
          // 清空二级分类
          this.courseInfo.subjectId = "";
        }
      });
    },

    // 上传成功后的回调函数
    cropSuccess(data) {
      console.log(data);
      this.imagecropperShow = false;
      this.courseInfo.cover = data.url;
      // 上传成功后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
      this.imagecropperKey = this.imagecropperKey + 1;
    },
    // 关闭上传组件
    close() {
      this.imagecropperShow = false;
      // 上传失败后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
      this.imagecropperKey = this.imagecropperKey + 1;
    },
  },
};
</script><
  style scoped>
.tinymce-container {
  line-height: 29px;
}
</style>